
#include "../include/head.h"
int assist(char *query,char* filename)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	//char query[300] = "select * from user where id='";
	unsigned int queryRet;
	
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
		{
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
                    if(!(strcmp(row[queryRet],filename)))
                    {
                        //如果找到了就，直接返回1
                        printf("客户端有该文件\n");
                        return 1;
                    }
					//printf("%8s ", row[queryRet]);
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}
typedef struct mysqlData
{
    int parent_id,owner_id,filesize,type;
    char name[30];
    char salt[30];
    char cryptpasswd[20];
    char filename[50];
    char md5[40];
    char pwd[80];

} UserData_t, *pUserData_t;
int selectData1(char* fileid,char *table, char *type, char *data,int fd)
{

    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char *server = "localhost";
    char *user = "chen";
    char *password = "123";
    char *database = "37chen"; //要访问的数据库名称
                               //char query[300] = "select * from user where id='";
    char query[300] = "select ";
    strcat(query,fileid);
    strcat(query," from ");
    //nt dp=dup2(fd,STDOUT_FILENO);
    //perror("dp dup2");
    //* from ";
    unsigned int queryRet;
    //sprintf(query, "%s%s%s%s", query, buf1,argv[1], "'");

    char buf2[20] = " where ";

    strcat(type, "='");

    strcat(data, "'");

    sprintf(query, "%s%s%s%s%s", query, table, buf2, type, data);
    //strcpy(query,"select * from user");

    //在输出前先打印查询语句
    printf("%s\n",query);

    //初始化
    conn = mysql_init(NULL);
    if (!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database: %s\t", mysql_error(conn));
        return -1;
    }
    else
    {
        
    //    printf("MySQL Connected...\n");
    }

    //把SQL语句传递给MySQL
    queryRet = mysql_query(conn, query);
    if (queryRet)
    {
        printf("Error making query: %s\n", mysql_error(conn));
    }
    else
    {

        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
       // printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
        /* res = mysql_use_result(conn); */

        row = mysql_fetch_row(res);
         
        if (NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
        {
            do
            {
                /* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
               
                for (queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
                {
                    //dup2(STDOUT_FILENO,fd);
                    strcat(row[queryRet],"\n");
                    int ret = send(fd,row[queryRet],strlen(row[queryRet]),0);
                    //int ret = write(fd,row[queryRet],strlen(row[queryRet]));
                   // perror("ret");
                  
                    printf("%s", row[queryRet]);
                }

                printf("\n");

            } while (NULL != (row = mysql_fetch_row(res)));
        }

        mysql_free_result(res);
    }

    mysql_close(conn);

    return 0;
}
int selectData(char* fileid,char *table, char *type, char *data)
{

    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char *server = "localhost";
    char *user = "chen";
    char *password = "123";
    char *database = "37chen"; //要访问的数据库名称
                               //char query[300] = "select * from user where id='";
    char query[300] = "select ";
    strcat(query,fileid);
    strcat(query," from ");

    //* from ";
    unsigned int queryRet;
    //sprintf(query, "%s%s%s%s", query, buf1,argv[1], "'");

    char buf2[20] = " where ";

    strcat(type, "='");

    strcat(data, "'");

    sprintf(query, "%s%s%s%s%s", query, table, buf2, type, data);
    //strcpy(query,"select * from user");
    
    //在输出前先打印查询语句
    //printf("%s\n",query);

    //初始化
    conn = mysql_init(NULL);
    if (!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database: %s\t", mysql_error(conn));
        return -1;
    }
    else
    {
        
    //    printf("MySQL Connected...\n");
    }

    //把SQL语句传递给MySQL
    queryRet = mysql_query(conn, query);
    if (queryRet)
    {
        printf("Error making query: %s\n", mysql_error(conn));
    }
    else
    {

        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
       // printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
        /* res = mysql_use_result(conn); */

        row = mysql_fetch_row(res);
         
        if (NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
        {
            do
            {
                /* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
               
                for (queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
                {
                    
                    printf("%s", row[queryRet]);
                }

                printf("\n");

            } while (NULL != (row = mysql_fetch_row(res)));
        }

        mysql_free_result(res);
    }

    mysql_close(conn);

    return 0;
}
int insertData(char *table, UserData_t userData)
{

    MYSQL *conn;
    char *server = "localhost";
    char *user = "chen";
    char *password = "123";
    char *database = "37chen"; //要访问的数据库名称
    int queryResult;
    //char query[200]="insert into member (id, age, english) values (5,123,100)";
    char query[500];
    //strcat(query,table);
    char buf[500];
    if (!strcmp(table, "user"))
    {
        //printf("user\n");
        
        strcpy(buf,"insert into user(username,salt,cryptpasswd,pwd) values ('");

        strcat(query, buf);
        //char query[200]="insert into user (username,salt,cryptpasswd,pwd) values ('chensiyuan11','122','1234','~/temp')";

        sprintf(query, "%s','%s','%s','%s');", userData.name, userData.salt, userData.cryptpasswd, userData.pwd);
        strcat(buf, query);
        puts(buf);
    }

    else
    {
       // printf("file\n");
        strcpy(buf,"insert into file(parent_id,filename,owner_id,md5,filesize,type) values ('");
        strcat(query,buf);
        char temp[10];
        puts(buf);
        sprintf(temp,"%d",userData.parent_id);
        char temp1[10];
        sprintf(temp1,"%d",userData.owner_id);
        char temp2[10];
        sprintf(temp2,"%d",userData.filesize);
        char temp3[2];
        sprintf(temp3,"%d",userData.type);
        sprintf(query,"%s','%s','%s','%s','%s','%s');",temp,userData.filename,temp1,userData.md5,temp2,temp3);
        strcat(buf,query);
        puts(buf);
    }
    conn = mysql_init(NULL);

    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database: %s\n", mysql_error(conn));
    }
    else
    {
        printf("Connected...\n");
    }

    queryResult = mysql_query(conn, buf);
    if (queryResult)
    {
        printf("Error making query:%s\n", mysql_error(conn));
    }
    else
    {
        printf("insert success\n");
    }
    mysql_close(conn);

    return 0;
}
int deleteData(char *table, char *type, char *data)
{
    MYSQL *conn;

    char *server = "localhost";
    char *user = "chen";
    char *password = "123";
    char *database = "37chen";
    char query[200] = "delete from user where id='3'";

    int queryRet;

    conn = mysql_init(NULL);

    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database: %s\n", mysql_error(conn));
    }
    else
    {
        printf("Connected...\n");
    }

    queryRet = mysql_query(conn, query);
    if (queryRet)
    {
        printf("Error making query:%s\n", mysql_error(conn));
    }
    else
    {
        unsigned long ret = mysql_affected_rows(conn);
        if (ret)
        {
            printf("delete success, delete row=%lu\n", ret);
        }
        else
        {
            printf("delete failed,mysql_affected_rows: %lu\n", ret);
        }
    }

    mysql_close(conn);

    return 0;
}
int selectFilename(char *filename,char* userID)
{
    //根据传入参数，查询数据
    char query[300]="select filename from file where owner_id='";
    strcat(query,userID);
    strcat(query,"'");
   
    int ret = assist(query,filename);
   
    return ret;
}
int selectpwd(char* query,int fd)
{
	
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	
	unsigned int queryRet;
	
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
        {
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
						printf("%s", row[queryRet]);
                        send(fd,row[queryRet],strlen(row[queryRet]),0);
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}
int selectls(char *query,int fd)
{
    char ls[200]={0};
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	
	unsigned int queryRet;
	
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
        {
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
						//printf("%s", row[queryRet]);
                        //send(fd,row[queryRet],strlen(row[queryRet]),0);
                        strcat(ls,row[queryRet]);
                        strcat(ls,"\t");
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}
    printf("ls %s\n",ls);
    send(fd,ls,strlen(ls),0);
	mysql_close(conn);

	return 0;
}
int selectcd(char* query,char* files)
{
    char ls[200]={0};
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	
	unsigned int queryRet;
	
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            return 0;
            printf("Don't find any data\n");
        }
        else
        {
            char buf[30]={0};
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
						//printf("%s", row[queryRet]);
                        //send(fd,row[queryRet],strlen(row[queryRet]),0);
                        if(!(strcmp(row[queryRet],files)))
                        {
                            return 1;
                        }
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}
 
	mysql_close(conn);

	return 0;
}
int updatePwd(char* query)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char* server="localhost";
	char* user="chen";
	char* password="123";
	char* database="37chen";
	
	puts(query);

	int queryResult;

	conn=mysql_init(NULL);

	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
	{
		printf("Error connecting to database:%s\n",mysql_error(conn));
	}
    else
    {
		printf("Connected...\n");
	}

	queryResult = mysql_query(conn, query);
	if(queryResult)
	{
		printf("Error making query:%s\n",mysql_error(conn));
	}
    else
    {
        int ret = mysql_affected_rows(conn);
        if(ret)
        {
            printf("update success\n");
        }
        else
        {
            printf("update fail, mysql_affected_rows:%d\n", ret);
        }
	}
	mysql_close(conn);
	return 0;
}
char* updateparent_id(char *query)
{
    char ls[200]={0};
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	
	unsigned int queryRet;
	
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return NULL;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return NULL;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
        {
            char buf[30]={0};
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
						//printf("%s", row[queryRet]);
                        //send(fd,row[queryRet],strlen(row[queryRet]),0);
                        return row[queryRet];
                        
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}
 
	mysql_close(conn);
    return NULL;
}
int moveCnt(char* query)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char* server="localhost";
	char* user="chen";
	char* password="123";
	char* database="37chen";
	
	

	int queryResult;

	conn=mysql_init(NULL);

	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
	{
		printf("Error connecting to database:%s\n",mysql_error(conn));
	}
    else
    {
		printf("Connected...\n");
	}

	queryResult = mysql_query(conn, query);
	if(queryResult)
	{
		printf("Error making query:%s\n",mysql_error(conn));
	}
    else
    {
        int ret = mysql_affected_rows(conn);
        if(ret)
        {
            printf("修改引用计数成功\n");
            printf("update success\n");
            return 1;
        }
        else
        {
            printf("update fail, mysql_affected_rows:%d\n", ret);
        }
	}
	mysql_close(conn);
	return 0;
}
int deleteCnt(char* query)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char* server="localhost";
	char* user="chen";
	char* password="123";
	char* database="37chen";
	

	int queryResult;

	conn=mysql_init(NULL);

	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
	{
		printf("Error connecting to database:%s\n",mysql_error(conn));
	}
    else
    {
		printf("Connected...\n");
	}

	queryResult = mysql_query(conn, query);
	if(queryResult)
	{
		printf("Error making query:%s\n",mysql_error(conn));
        return 0;
	}
    else
    {
        int ret = mysql_affected_rows(conn);
        if(ret)
        {
            printf("update success\n");
            return 1;
        }
        else
        {
            printf("update fail, mysql_affected_rows:%d\n", ret);
        }
	}
	mysql_close(conn);
	return 0;
}
int Mkdir(char* query)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	//char query[300] = "select * from user where id='";
	unsigned int queryRet;
	//sprintf(query, "%s%s%s", query, argv[1], "'");
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            
            printf("Don't find any data\n");
            return 0;
        }
        else
		{
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
						//printf("%8s ", row[queryRet]);
                        return 1;//找到了就不能创建
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}
int selectMkdir(char* parent_id,char* userId,char* filename)
{
    //select filename from file where owner_id=1 and parent_id=12 and filename='caonima';

    char query[100]="select filename from file where owner_id='";
    sprintf(query,"%s%s' and parent_id='%s' and filename='%s'",query,userId,parent_id,filename);
    printf("查询目录条件语句是%s\n",query);
    return Mkdir(query);
}
int insertMkdir(char* query)
{
	MYSQL *conn;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
    //char query[200]="insert into member (id, age, english) values (5,123,100)";
    //char query[200]="insert into user (username,salt,cryptpasswd,pwd) values ('chensiyuan','100','1234','~/temp')";
	int queryResult;

	conn = mysql_init(NULL);

	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
	}
    else
    {
		printf("Connected...\n");
	}

	queryResult = mysql_query(conn, query);
	if(queryResult)
	{
		printf("Error making query:%s\n", mysql_error(conn));
	}
    else
    {
		printf("insert success\n");
	}
	mysql_close(conn);

	return 0;
}
int selectMd5(char* query,char* userid)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	//char query[300] = "select * from user where id='";
	unsigned int queryRet;
	//sprintf(query, "%s%s%s", query, argv[1], "'");
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));
        if((unsigned long)mysql_num_rows(res)>0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            
            printf("Don't find any data\n");
            return 0;
        }
        else
		{
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
						printf("%8s ", row[queryRet]);
                        return 1;//找到了就不能创建
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}